package fr.ysa.persistence.requetes.filtres.utils;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import fr.ysa.persistence.requetes.filtres.Filtre;
import fr.ysa.persistence.requetes.filtres.ValueFiltre;

public class FiltreValueNumber {

	public String getValue(Filtre filtre) {
		String queryFiltres = "";
		String operation = " = ";
				int indV = 0;
				for (ValueFiltre value : filtre.getValues()) {
					switch (value.getOperation()) {
					case EQUAL:
						operation = " = ";
						break;

					case INF:
						operation = " < ";
						break;

					case SUP:
						operation = " > ";
						break;

					case ESUP:
						operation = " >= ";
						break;

					case EINF:
						operation = " <= ";
						break;

					default:
						break;
					}

					queryFiltres += filtre.getName() + operation
							+ value.getValue();
					indV++;
					if (indV < filtre.getValues().size()) {
						queryFiltres += " AND ";
					}
				}
		return queryFiltres;
	}

	public List<SimpleExpression> getRestrictionsExpression(Filtre filtre) {
		List<SimpleExpression> restrictions = new ArrayList<SimpleExpression>();
		SimpleExpression expression;
			for (ValueFiltre value : filtre.getValues()) {
				switch (value.getOperation()) {
				case EQUAL:
					expression = Restrictions.eq(filtre.getName(), value.getValue());
					break;

				case INF:
					expression = Restrictions.lt(filtre.getName(), value.getValue());
					break;

				case SUP:
					expression = Restrictions.gt(filtre.getName(), value.getValue());
					break;

				case ESUP:
					expression = Restrictions.ge(filtre.getName(), value.getValue());
					break;

				case EINF:
					expression = Restrictions.le(filtre.getName(), value.getValue());
					break;

				default:
					expression = Restrictions.eq(filtre.getName(), value.getValue());
					break;
				}
				restrictions.add(expression);
			}
		return restrictions;
	}

}
